package org.zuoye.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Timestamp;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.zuoye.Dao.uploadDao;
import org.zuoye.DaoImp.uploadDaoimp;
import org.zuoye.bean.UserLogin;
import org.zuoye.bean.Zuoye;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class uploadAction extends ActionSupport{
	

	private  File file;           		 		  		//上传文件
    private String fileFileName;						//上传的文件名  		
	private String message="上传失败";					//定义上传提示信息
	private String zuoyeName;
	uploadDao upDao=new uploadDaoimp();
	//属性file的get/set方法
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	public String getZuoyeName() {
		return zuoyeName;
	}
	public void setZuoyeName(String zuoyeName) {
		this.zuoyeName = zuoyeName;
	}
	
	public String execute() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();	//获得会话，用来获取当前登录的用户
		UserLogin  user=(UserLogin) session.get("login");
		String userNAME=user.getUserName();
//		System.out.println(user);
		String path = ServletActionContext.getServletContext().getRealPath("/upload/"+getFileFileName());
//		String url="E:\\apache-tomcat-8.0.30\\webapps\\zuoyeManage\\upload\\"+getFileFileName();
//		Timestamp d = new Timestamp(System.currentTimeMillis());
		Zuoye addZuoye = new Zuoye();
		addZuoye.setUserName(userNAME);
		addZuoye.setZuoyeName(zuoyeName);
		addZuoye.setZuoyeUpdate(new Timestamp(System.currentTimeMillis()));
		addZuoye.setZuoyeUrl(path);
		System.out.println(addZuoye);
		InputStream is=new FileInputStream(getFile());    //根据上传的文件得到输入流
		OutputStream os=new FileOutputStream(path);  	  //指定输出流地址
		byte buffer[]=new byte[1024];   
		int count=0;
		while((count=is.read(buffer))>0){
			os.write(buffer,0,count);    			      //把文件写到指定位置的文件中
		}
		os.close();                    			   		  //关闭
		is.close();
		setMessage("上传成功");
		upDao.addZuoye(addZuoye);
		return SUCCESS;                          		  //返回
	}
	//属性uploadFileName的get/set方法
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
}
